//
//  LWBadgeBtn.m
//  1.28  weibo
//
//  Created by mac on 15/2/27.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWBadgeBtn.h"
#import "UIImage+MJ.h"
@implementation LWBadgeBtn

- (instancetype)initWithFrame:(CGRect)frame{
 
    self = [super initWithFrame:frame];
    if (self) {
        
        self.hidden = YES;
        self.userInteractionEnabled = NO;
        [self setBackgroundImage:[UIImage resizedImageWithName:@"main_badge"] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (void)setBadgeValue:(NSString *)badgeValue{

    _badgeValue = badgeValue;
    
    if (badgeValue.intValue > 0 ) {
        self.hidden = NO;
        if (badgeValue.integerValue > 99) {
    
            CGRect frame = self.frame;
            CGFloat badgeH = self.currentBackgroundImage.size.height;
            CGFloat badgeW = self.currentBackgroundImage.size.width;
            
            NSString *badgeString = @"99+";
            NSDictionary *attributes = @{NSFontAttributeName: self.titleLabel.font};
            CGSize badgeSize = [badgeString sizeWithAttributes:attributes];
            badgeW = badgeSize.width + 10;
            
            frame.size.width = badgeW;
            frame.size.height = badgeH;
            self.frame = frame;
            
            [self setTitle:badgeString forState:UIControlStateNormal];
            
        }else{
            
            CGRect frame = self.frame;
            CGFloat badgeH = self.currentBackgroundImage.size.height;
            CGFloat badgeW = self.currentBackgroundImage.size.width;
            
            if (badgeValue.intValue >= 10) {
                NSString *badgeString = [NSString stringWithFormat:@"%@",badgeValue];
                NSDictionary *attributes = @{NSFontAttributeName: self.titleLabel.font};
                CGSize badgeSize = [badgeString sizeWithAttributes:attributes];
                badgeW = badgeSize.width + 15;
            }
            
            frame.size.width = badgeW;
            frame.size.height = badgeH;
            self.frame = frame;
            
            [self setTitle:[badgeValue description] forState:UIControlStateNormal];
            
            }
        
    }else{
        self.hidden = YES;
    }

}

@end
